﻿using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;


namespace Module_Debug.Visionox_Gamma
{
    public partial class CH13721_Gamma : Form
    {
        CaSDK CaSDKObj = new CaSDK();
        CRC16 CRCData = new CRC16();

        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke 

        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过

        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色
        private double D_x, D_y;
        private double lum_off, x_off, y_off;
        private int index = 0;
        private int flag1 = 1; //lum
        private int flag2 = 1; //x
        private int flag3 = 1; //y
        private byte[] Send_buf = new byte[1];
        private byte[] Rec_tmp = new byte[1];

        private static bool Gamma_adjust_timeout_flag = false;
        public byte CH13721_0XFE_XX = 0;
        private int adjust_Rnum = 0;
        private int adjust_Gnum = 0;
        private int adjust_Bnum = 0;
        public int adjust_l = 0;
        public int adjust_x = 0;
        public int adjust_y = 0;
        private int step_l = 1;
        //private int step_m = 5;
        private int step_h = 12;

        //监听CH13721挂载
        frmMain frmHelp = null;
        public CH13721_Gamma(frmMain CH13721)
        {
            frmHelp = CH13721;
            InitializeComponent();
            frmHelp.T20.Interval = 200; //采样周期设定
            frmHelp.T20.Tick += new EventHandler(CH13721_auto_gamma);
            Thread.Sleep(5);
            frmHelp.Child_window_hide_already = true;
            frmHelp.Child_Gamma_Window_Open_Enable = false;//防止重复开启子窗体  
        }
        //CH13721 监听卸载
        private void CH13721_Gamma_FormClosed(object sender, FormClosedEventArgs e)
        {
            ////frmHelp.comPort.Close()
            Thread.Sleep(5);
            frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(CH13721_DataReceived);
            Thread.Sleep(5);
            frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
            Thread.Sleep(5);
            ////frmHelp.comPort.Open();

            frmHelp.T20.Tick -= new EventHandler(CH13721_auto_gamma);
            Thread.Sleep(5);
            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "CH13721_Gamma_FormClosed！\r\n";
            frmHelp.Child_Gamma_Window_Open_Enable = true; //允许其他子窗体打开
            frmHelp.Child_window_hide_already = false;
            frmHelp.IC_comboBox.Enabled = true;
        }

        private void CH13721_Gamma_VisibleChanged(object sender, EventArgs e)
        {
            if (frmHelp.Child_window_hide_already == false) //子窗体已开启标识
            {
                ////frmHelp.comPort.Close()
                Thread.Sleep(5);
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(CH13721_DataReceived);
                Thread.Sleep(5);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                Thread.Sleep(5);
                ////frmHelp.comPort.Open();
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "CH13721_Gamma_FormHide！\r\n";
                frmHelp.Child_window_hide_already = true; //子窗体已隐藏标识
            }
            else
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                Thread.Sleep(5);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(CH13721_DataReceived);
                Thread.Sleep(5);
                frmHelp.T20.Tick += new EventHandler(CH13721_auto_gamma);
                Thread.Sleep(5);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "CH13721_Gamma_FormOpened！\r\n";
                frmHelp.Child_window_hide_already = false; //子窗体已开启标识
            }
        }

        private void CH13721_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            if (IClosing) return;
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = frmHelp.comPort.BytesToRead;
                byte[] buf = new byte[n];
                byte data;

                frmHelp.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    // 0x4A: CH13721的Gamma读写、寄存器读写、OTP读写    设置  返回
                    if (buffer[0] == 0x4A && Setting.TempRecord.cmdLen > 0)
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, Setting.TempRecord.cmdLen);//正确分析一条数据，从缓存中移除数据。 
                            }

                        }
                    }
                    else
                    {
                        buffer.RemoveRange(0, buffer.Count);
                    }
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        frmHelp.T1.Enabled = false; //停止超时计数
                        if (Rec_tmp[0] == 0x4A)      //CH13721
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写寄存器\tOK！\r\n";
                                    }
                                    break;
                                case 0x02:
                                    {
                                        if (Setting.TempRecord.CH13721CmdType == 0x02)  //register read
                                        {
                                            string tmp = "";
                                            for (int i = 3; i < (Setting.TempRecord.cmdLen - 3); i++)
                                            {
                                                tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]) + ",";
                                            }
                                            tmp = tmp + String.Format("{0:X2}", Rec_tmp[Setting.TempRecord.cmdLen - 3]);
                                            CH13721_txtRegData.Text = tmp;
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "寄存器\t读 OK！\r\n";
                                        }
                                    }
                                    break;

                                case 0x08:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            if ((Rec_tmp[3] & 0xf0) == 0x00)
                                                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma数据\t写 OK！\r\n";
                                            else if ((Rec_tmp[3] & 0xf0) == 0x40)
                                                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma数据\t写 OK！\r\n";
                                            else if ((Rec_tmp[3] & 0xf0) == 0x80)
                                                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma数据\t写 OK！\r\n";
                                        }
                                    }
                                    break;

                                case 0x11:
                                    {
                                        switch (Rec_tmp[3])
                                        {
                                            case 0x01:
                                            case 0x02:
                                            case 0x03:
                                                {
                                                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Gamma切换成功！\r\n";
                                                }
                                                break;
                                            default: break;
                                        }

                                    }
                                    break;

                                case 0x0A:
                                    {
                                        if (Rec_tmp[3] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP \t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP \t写 ERROR！\r\n";
                                    }
                                    break;

                                case 0x0D:
                                    {
                                        if (Rec_tmp[3] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP HBM \t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP HBM \t写 ERROR！\r\n";
                                    }
                                    break;

                                case 0x0E:
                                    {
                                        if (Rec_tmp[3] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP GOA \t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP GOA \t写 ERROR！\r\n";
                                    }
                                    break;

                                case 0x0F:
                                    {
                                        if (Rec_tmp[3] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP ID \t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP ID \t写 ERROR！\r\n";
                                    }
                                    break;

                                case 0x09:
                                    {
                                        if (Setting.TempRecord.CH13721CmdType == 0x031)
                                        {
                                            CH13721_R0.Value = Decimal.Parse((Rec_tmp[4] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            CH13721_R1.Value = Decimal.Parse(((Rec_tmp[4] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[6].ToString());
                                            CH13721_R3.Value = Decimal.Parse(((Rec_tmp[4] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            CH13721_R4.Value = Decimal.Parse(((Rec_tmp[4] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[8].ToString());

                                            CH13721_R8.Value = Decimal.Parse((Rec_tmp[9] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[10].ToString());
                                            CH13721_R12.Value = Decimal.Parse(((Rec_tmp[9] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            CH13721_R16.Value = Decimal.Parse(((Rec_tmp[9] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[12].ToString());
                                            CH13721_R20.Value = Decimal.Parse(((Rec_tmp[9] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());

                                            CH13721_R28.Value = Decimal.Parse((Rec_tmp[14] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            CH13721_R36.Value = Decimal.Parse(((Rec_tmp[14] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[16].ToString());
                                            CH13721_R44.Value = Decimal.Parse(((Rec_tmp[14] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            CH13721_R52.Value = Decimal.Parse(((Rec_tmp[14] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[18].ToString());
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma1数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.CH13721CmdType == 0x32)
                                        {
                                            CH13721_R68.Value = Decimal.Parse((Rec_tmp[4] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            CH13721_R84.Value = Decimal.Parse(((Rec_tmp[4] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[6].ToString());
                                            CH13721_R100.Value = Decimal.Parse(((Rec_tmp[4] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            CH13721_R116.Value = Decimal.Parse(((Rec_tmp[4] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[8].ToString());

                                            CH13721_R124.Value = Decimal.Parse((Rec_tmp[9] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[10].ToString());
                                            CH13721_R132.Value = Decimal.Parse(((Rec_tmp[9] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            CH13721_R148.Value = Decimal.Parse(((Rec_tmp[9] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[12].ToString());
                                            CH13721_R164.Value = Decimal.Parse(((Rec_tmp[9] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());

                                            CH13721_R180.Value = Decimal.Parse((Rec_tmp[14] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            CH13721_R188.Value = Decimal.Parse(((Rec_tmp[14] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[16].ToString());
                                            CH13721_R196.Value = Decimal.Parse(((Rec_tmp[14] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            CH13721_R204.Value = Decimal.Parse(((Rec_tmp[14] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[18].ToString());
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma2数据\t读 OK！\r\n";

                                        }
                                        else if (Setting.TempRecord.CH13721CmdType == 0x33)
                                        {
                                            CH13721_R212.Value = Decimal.Parse((Rec_tmp[4] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            CH13721_R220.Value = Decimal.Parse(((Rec_tmp[4] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[6].ToString());
                                            CH13721_R228.Value = Decimal.Parse(((Rec_tmp[4] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            CH13721_R236.Value = Decimal.Parse(((Rec_tmp[4] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[8].ToString());

                                            CH13721_R244.Value = Decimal.Parse((Rec_tmp[9] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[10].ToString());
                                            CH13721_R248.Value = Decimal.Parse(((Rec_tmp[9] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            CH13721_R250.Value = Decimal.Parse(((Rec_tmp[9] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[12].ToString());
                                            CH13721_R252.Value = Decimal.Parse(((Rec_tmp[9] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());

                                            CH13721_R254.Value = Decimal.Parse((Rec_tmp[14] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            CH13721_R255.Value = Decimal.Parse(((Rec_tmp[14] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[16].ToString());
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma3数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.CH13721CmdType == 0x41)
                                        {
                                            CH13721_G0.Value = Decimal.Parse((Rec_tmp[4] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            CH13721_G1.Value = Decimal.Parse(((Rec_tmp[4] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[6].ToString());
                                            CH13721_G3.Value = Decimal.Parse(((Rec_tmp[4] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            CH13721_G4.Value = Decimal.Parse(((Rec_tmp[4] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[8].ToString());

                                            CH13721_G8.Value = Decimal.Parse((Rec_tmp[9] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[10].ToString());
                                            CH13721_G12.Value = Decimal.Parse(((Rec_tmp[9] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            CH13721_G16.Value = Decimal.Parse(((Rec_tmp[9] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[12].ToString());
                                            CH13721_G20.Value = Decimal.Parse(((Rec_tmp[9] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());

                                            CH13721_G28.Value = Decimal.Parse((Rec_tmp[14] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            CH13721_G36.Value = Decimal.Parse(((Rec_tmp[14] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[16].ToString());
                                            CH13721_G44.Value = Decimal.Parse(((Rec_tmp[14] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            CH13721_G52.Value = Decimal.Parse(((Rec_tmp[14] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[18].ToString());

                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma1数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.CH13721CmdType == 0x42)
                                        {
                                            CH13721_G68.Value = Decimal.Parse((Rec_tmp[4] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            CH13721_G84.Value = Decimal.Parse(((Rec_tmp[4] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[6].ToString());
                                            CH13721_G100.Value = Decimal.Parse(((Rec_tmp[4] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            CH13721_G116.Value = Decimal.Parse(((Rec_tmp[4] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[8].ToString());

                                            CH13721_G124.Value = Decimal.Parse((Rec_tmp[9] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[10].ToString());
                                            CH13721_G132.Value = Decimal.Parse(((Rec_tmp[9] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            CH13721_G148.Value = Decimal.Parse(((Rec_tmp[9] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[12].ToString());
                                            CH13721_G164.Value = Decimal.Parse(((Rec_tmp[9] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());

                                            CH13721_G180.Value = Decimal.Parse((Rec_tmp[14] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            CH13721_G188.Value = Decimal.Parse(((Rec_tmp[14] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[16].ToString());
                                            CH13721_G196.Value = Decimal.Parse(((Rec_tmp[14] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            CH13721_G204.Value = Decimal.Parse(((Rec_tmp[14] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[18].ToString());
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma2数据\t读 OK！\r\n";

                                        }
                                        else if (Setting.TempRecord.CH13721CmdType == 0x43)
                                        {
                                            CH13721_G212.Value = Decimal.Parse((Rec_tmp[4] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            CH13721_G220.Value = Decimal.Parse(((Rec_tmp[4] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[6].ToString());
                                            CH13721_G228.Value = Decimal.Parse(((Rec_tmp[4] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            CH13721_G236.Value = Decimal.Parse(((Rec_tmp[4] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[8].ToString());

                                            CH13721_G244.Value = Decimal.Parse((Rec_tmp[9] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[10].ToString());
                                            CH13721_G248.Value = Decimal.Parse(((Rec_tmp[9] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            CH13721_G250.Value = Decimal.Parse(((Rec_tmp[9] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[12].ToString());
                                            CH13721_G252.Value = Decimal.Parse(((Rec_tmp[9] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());

                                            CH13721_G254.Value = Decimal.Parse((Rec_tmp[14] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            CH13721_G255.Value = Decimal.Parse(((Rec_tmp[14] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[16].ToString());
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma3数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.CH13721CmdType == 0x51)
                                        {
                                            CH13721_B0.Value = Decimal.Parse((Rec_tmp[4] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            CH13721_B1.Value = Decimal.Parse(((Rec_tmp[4] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[6].ToString());
                                            CH13721_B3.Value = Decimal.Parse(((Rec_tmp[4] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            CH13721_B4.Value = Decimal.Parse(((Rec_tmp[4] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[8].ToString());

                                            CH13721_B8.Value = Decimal.Parse((Rec_tmp[9] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[10].ToString());
                                            CH13721_B12.Value = Decimal.Parse(((Rec_tmp[9] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            CH13721_B16.Value = Decimal.Parse(((Rec_tmp[9] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[12].ToString());
                                            CH13721_B20.Value = Decimal.Parse(((Rec_tmp[9] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());

                                            CH13721_B28.Value = Decimal.Parse((Rec_tmp[14] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            CH13721_B36.Value = Decimal.Parse(((Rec_tmp[14] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[16].ToString());
                                            CH13721_B44.Value = Decimal.Parse(((Rec_tmp[14] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            CH13721_B52.Value = Decimal.Parse(((Rec_tmp[14] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[18].ToString());

                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma1数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.CH13721CmdType == 0x52)
                                        {
                                            CH13721_B68.Value = Decimal.Parse((Rec_tmp[4] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            CH13721_B84.Value = Decimal.Parse(((Rec_tmp[4] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[6].ToString());
                                            CH13721_B100.Value = Decimal.Parse(((Rec_tmp[4] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            CH13721_B116.Value = Decimal.Parse(((Rec_tmp[4] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[8].ToString());

                                            CH13721_B124.Value = Decimal.Parse((Rec_tmp[9] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[10].ToString());
                                            CH13721_B132.Value = Decimal.Parse(((Rec_tmp[9] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            CH13721_B148.Value = Decimal.Parse(((Rec_tmp[9] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[12].ToString());
                                            CH13721_B164.Value = Decimal.Parse(((Rec_tmp[9] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());

                                            CH13721_B180.Value = Decimal.Parse((Rec_tmp[14] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            CH13721_B188.Value = Decimal.Parse(((Rec_tmp[14] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[16].ToString());
                                            CH13721_B196.Value = Decimal.Parse(((Rec_tmp[14] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            CH13721_B204.Value = Decimal.Parse(((Rec_tmp[14] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[18].ToString());

                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma2数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.CH13721CmdType == 0x53)
                                        {
                                            CH13721_B212.Value = Decimal.Parse((Rec_tmp[4] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            CH13721_B220.Value = Decimal.Parse(((Rec_tmp[4] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[6].ToString());
                                            CH13721_B228.Value = Decimal.Parse(((Rec_tmp[4] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            CH13721_B236.Value = Decimal.Parse(((Rec_tmp[4] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[8].ToString());

                                            CH13721_B244.Value = Decimal.Parse((Rec_tmp[9] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[10].ToString());
                                            CH13721_B248.Value = Decimal.Parse(((Rec_tmp[9] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            CH13721_B250.Value = Decimal.Parse(((Rec_tmp[9] >> 4) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[12].ToString());
                                            CH13721_B252.Value = Decimal.Parse(((Rec_tmp[9] >> 6) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());

                                            CH13721_B254.Value = Decimal.Parse((Rec_tmp[14] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            CH13721_B255.Value = Decimal.Parse(((Rec_tmp[14] >> 2) & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[16].ToString());

                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma3数据\t读 OK！\r\n";
                                        }

                                    }
                                    break;
                                default: break;

                            }
                        }
                        frmHelp.textBox1.SelectionStart = this.frmHelp.textBox1.Text.Length;
                        frmHelp.textBox1.ScrollToCaret();//滚动到光标处
                        if (frmHelp.textBox1.Lines.Length > 50) frmHelp.textBox1.Text = null;
                    }));
                }
            }
            finally
            {
                Listening = false;
            }
        }
        //Gamma保存
        private void CH13721_SaveGamma_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "保存Gamma文件";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    str = str + "R0," + CH13721_R0.Text + "," + "G0," + CH13721_G0.Text + "," + "B0," + CH13721_B0.Text + "\r\n";
                    str = str + "R1," + CH13721_R1.Text + "," + "G1," + CH13721_G1.Text + "," + "B1," + CH13721_B1.Text + "\r\n";
                    str = str + "R3," + CH13721_R3.Text + "," + "G3," + CH13721_G3.Text + "," + "B3," + CH13721_B3.Text + "\r\n";
                    str = str + "R4," + CH13721_R4.Text + "," + "G4," + CH13721_G4.Text + "," + "B4," + CH13721_B4.Text + "\r\n";
                    str = str + "R8," + CH13721_R8.Text + "," + "G8," + CH13721_G8.Text + "," + "B8," + CH13721_B8.Text + "\r\n";
                    str = str + "R12," + CH13721_R12.Text + "," + "G12," + CH13721_G12.Text + "," + "B12," + CH13721_B12.Text + "\r\n";
                    str = str + "R16," + CH13721_R16.Text + "," + "G16," + CH13721_G16.Text + "," + "B16," + CH13721_B16.Text + "\r\n";
                    str = str + "R20," + CH13721_R20.Text + "," + "G20," + CH13721_G20.Text + "," + "B20," + CH13721_B20.Text + "\r\n";
                    str = str + "R28," + CH13721_R28.Text + "," + "G28," + CH13721_G28.Text + "," + "B28," + CH13721_B28.Text + "\r\n";
                    str = str + "R36," + CH13721_R36.Text + "," + "G36," + CH13721_G36.Text + "," + "B36," + CH13721_B36.Text + "\r\n";
                    str = str + "R44," + CH13721_R44.Text + "," + "G44," + CH13721_G44.Text + "," + "B44," + CH13721_B44.Text + "\r\n";
                    str = str + "R52," + CH13721_R52.Text + "," + "G52," + CH13721_G52.Text + "," + "B52," + CH13721_B52.Text + "\r\n";
                    str = str + "R68," + CH13721_R68.Text + "," + "G68," + CH13721_G68.Text + "," + "B68," + CH13721_B68.Text + "\r\n";
                    str = str + "R84," + CH13721_R84.Text + "," + "G84," + CH13721_G84.Text + "," + "B84," + CH13721_B84.Text + "\r\n";
                    str = str + "R100," + CH13721_R100.Text + "," + "G100," + CH13721_G100.Text + "," + "B100," + CH13721_B100.Text + "\r\n";
                    str = str + "R116," + CH13721_R116.Text + "," + "G116," + CH13721_G116.Text + "," + "B116," + CH13721_B116.Text + "\r\n";
                    str = str + "R124," + CH13721_R124.Text + "," + "G124," + CH13721_G124.Text + "," + "B124," + CH13721_B124.Text + "\r\n";
                    str = str + "R132," + CH13721_R132.Text + "," + "G132," + CH13721_G132.Text + "," + "B132," + CH13721_B132.Text + "\r\n";
                    str = str + "R148," + CH13721_R148.Text + "," + "G148," + CH13721_G148.Text + "," + "B148," + CH13721_B148.Text + "\r\n";
                    str = str + "R164," + CH13721_R164.Text + "," + "G164," + CH13721_G164.Text + "," + "B164," + CH13721_B164.Text + "\r\n";
                    str = str + "R180," + CH13721_R180.Text + "," + "G180," + CH13721_G180.Text + "," + "B180," + CH13721_B180.Text + "\r\n";
                    str = str + "R188," + CH13721_R188.Text + "," + "G188," + CH13721_G188.Text + "," + "B188," + CH13721_B188.Text + "\r\n";
                    str = str + "R196," + CH13721_R196.Text + "," + "G196," + CH13721_G196.Text + "," + "B196," + CH13721_B196.Text + "\r\n";
                    str = str + "R204," + CH13721_R204.Text + "," + "G204," + CH13721_G204.Text + "," + "B204," + CH13721_B204.Text + "\r\n";
                    str = str + "R212," + CH13721_R212.Text + "," + "G212," + CH13721_G212.Text + "," + "B212," + CH13721_B212.Text + "\r\n";
                    str = str + "R220," + CH13721_R220.Text + "," + "G220," + CH13721_G220.Text + "," + "B220," + CH13721_B220.Text + "\r\n";
                    str = str + "R228," + CH13721_R228.Text + "," + "G228," + CH13721_G228.Text + "," + "B228," + CH13721_B228.Text + "\r\n";
                    str = str + "R236," + CH13721_R236.Text + "," + "G236," + CH13721_G236.Text + "," + "B236," + CH13721_B236.Text + "\r\n";
                    str = str + "R244," + CH13721_R244.Text + "," + "G244," + CH13721_G244.Text + "," + "B244," + CH13721_B244.Text + "\r\n";
                    str = str + "R248," + CH13721_R248.Text + "," + "G248," + CH13721_G248.Text + "," + "B248," + CH13721_B248.Text + "\r\n";
                    str = str + "R250," + CH13721_R250.Text + "," + "G250," + CH13721_G250.Text + "," + "B250," + CH13721_B250.Text + "\r\n";
                    str = str + "R252," + CH13721_R252.Text + "," + "G252," + CH13721_G252.Text + "," + "B252," + CH13721_B252.Text + "\r\n";
                    str = str + "R254," + CH13721_R254.Text + "," + "G254," + CH13721_G254.Text + "," + "B254," + CH13721_B254.Text + "\r\n";
                    str = str + "R255," + CH13721_R255.Text + "," + "G255," + CH13721_G255.Text + "," + "B255," + CH13721_B255.Text + "\r\n";
                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        } //
        //Gamma载入
        private void CH13721_LoadGamma_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
                string[] arrtmp = strtmp.Split(new Char[] { ',' });
                CH13721_R0.Text = arrtmp[1].ToString(); CH13721_G0.Text = arrtmp[3].ToString(); CH13721_B0.Text = arrtmp[5].ToString();
                CH13721_R1.Text = arrtmp[7].ToString(); CH13721_G1.Text = arrtmp[9].ToString(); CH13721_B1.Text = arrtmp[11].ToString();
                CH13721_R3.Text = arrtmp[13].ToString(); CH13721_G3.Text = arrtmp[15].ToString(); CH13721_B3.Text = arrtmp[17].ToString();
                CH13721_R4.Text = arrtmp[19].ToString(); CH13721_G4.Text = arrtmp[21].ToString(); CH13721_B4.Text = arrtmp[23].ToString();
                CH13721_R8.Text = arrtmp[25].ToString(); CH13721_G8.Text = arrtmp[27].ToString(); CH13721_B8.Text = arrtmp[29].ToString();
                CH13721_R12.Text = arrtmp[31].ToString(); CH13721_G12.Text = arrtmp[33].ToString(); CH13721_B12.Text = arrtmp[35].ToString();
                CH13721_R16.Text = arrtmp[37].ToString(); CH13721_G16.Text = arrtmp[39].ToString(); CH13721_B16.Text = arrtmp[41].ToString();
                CH13721_R20.Text = arrtmp[43].ToString(); CH13721_G20.Text = arrtmp[45].ToString(); CH13721_B20.Text = arrtmp[47].ToString();
                CH13721_R28.Text = arrtmp[49].ToString(); CH13721_G28.Text = arrtmp[51].ToString(); CH13721_B28.Text = arrtmp[53].ToString();
                CH13721_R36.Text = arrtmp[55].ToString(); CH13721_G36.Text = arrtmp[57].ToString(); CH13721_B36.Text = arrtmp[59].ToString();
                CH13721_R44.Text = arrtmp[61].ToString(); CH13721_G44.Text = arrtmp[63].ToString(); CH13721_B44.Text = arrtmp[65].ToString();
                CH13721_R52.Text = arrtmp[67].ToString(); CH13721_G52.Text = arrtmp[69].ToString(); CH13721_B52.Text = arrtmp[71].ToString();
                CH13721_R68.Text = arrtmp[73].ToString(); CH13721_G68.Text = arrtmp[75].ToString(); CH13721_B68.Text = arrtmp[77].ToString();
                CH13721_R84.Text = arrtmp[79].ToString(); CH13721_G84.Text = arrtmp[81].ToString(); CH13721_B84.Text = arrtmp[83].ToString();
                CH13721_R100.Text = arrtmp[85].ToString(); CH13721_G100.Text = arrtmp[87].ToString(); CH13721_B100.Text = arrtmp[89].ToString();
                CH13721_R116.Text = arrtmp[91].ToString(); CH13721_G116.Text = arrtmp[93].ToString(); CH13721_B116.Text = arrtmp[95].ToString();
                CH13721_R124.Text = arrtmp[97].ToString(); CH13721_G124.Text = arrtmp[99].ToString(); CH13721_B124.Text = arrtmp[101].ToString();
                CH13721_R132.Text = arrtmp[103].ToString(); CH13721_G132.Text = arrtmp[105].ToString(); CH13721_B132.Text = arrtmp[107].ToString();
                CH13721_R148.Text = arrtmp[109].ToString(); CH13721_G148.Text = arrtmp[111].ToString(); CH13721_B148.Text = arrtmp[113].ToString();
                CH13721_R164.Text = arrtmp[115].ToString(); CH13721_G164.Text = arrtmp[117].ToString(); CH13721_B164.Text = arrtmp[119].ToString();
                CH13721_R180.Text = arrtmp[121].ToString(); CH13721_G180.Text = arrtmp[123].ToString(); CH13721_B180.Text = arrtmp[125].ToString();
                CH13721_R188.Text = arrtmp[127].ToString(); CH13721_G188.Text = arrtmp[129].ToString(); CH13721_B188.Text = arrtmp[131].ToString();
                CH13721_R196.Text = arrtmp[133].ToString(); CH13721_G196.Text = arrtmp[135].ToString(); CH13721_B196.Text = arrtmp[137].ToString();
                CH13721_R204.Text = arrtmp[139].ToString(); CH13721_G204.Text = arrtmp[141].ToString(); CH13721_B204.Text = arrtmp[143].ToString();
                CH13721_R212.Text = arrtmp[145].ToString(); CH13721_G212.Text = arrtmp[147].ToString(); CH13721_B212.Text = arrtmp[149].ToString();
                CH13721_R220.Text = arrtmp[151].ToString(); CH13721_G220.Text = arrtmp[153].ToString(); CH13721_B220.Text = arrtmp[155].ToString();
                CH13721_R228.Text = arrtmp[157].ToString(); CH13721_G228.Text = arrtmp[159].ToString(); CH13721_B228.Text = arrtmp[161].ToString();
                CH13721_R236.Text = arrtmp[163].ToString(); CH13721_G236.Text = arrtmp[165].ToString(); CH13721_B236.Text = arrtmp[167].ToString();
                CH13721_R244.Text = arrtmp[169].ToString(); CH13721_G244.Text = arrtmp[171].ToString(); CH13721_B244.Text = arrtmp[173].ToString();
                CH13721_R248.Text = arrtmp[175].ToString(); CH13721_G248.Text = arrtmp[177].ToString(); CH13721_B248.Text = arrtmp[179].ToString();
                CH13721_R250.Text = arrtmp[181].ToString(); CH13721_G250.Text = arrtmp[183].ToString(); CH13721_B250.Text = arrtmp[185].ToString();
                CH13721_R252.Text = arrtmp[187].ToString(); CH13721_G252.Text = arrtmp[189].ToString(); CH13721_B252.Text = arrtmp[191].ToString();
                CH13721_R254.Text = arrtmp[193].ToString(); CH13721_G254.Text = arrtmp[195].ToString(); CH13721_B254.Text = arrtmp[197].ToString();
                CH13721_R255.Text = arrtmp[199].ToString(); CH13721_G255.Text = arrtmp[201].ToString(); CH13721_B255.Text = arrtmp[203].ToString();
            }
        }

        //------------------------------------------CH13721_START------------------------------------------
        private void CH13721_RegWR_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Int32 DataNum;
            string strLine = Convert.ToString(CH13721_txtRegData.Text);
            string[] arr = strLine.Split(','); //以逗号分割，添加到数组

            switch (btnTmp.Name)
            {
                case "CH13721_RegW":
                    {
                        Setting.TempRecord.cmdLen = arr.Length + 5;
                        Send_buf = new byte[arr.Length + 5];
                        Send_buf[0] = 0x4A;//CH13721

                        if (CH13721_rbt_LP.Checked == true)
                        { Send_buf[1] = 0x01; }
                        else if (CH13721_rbt_HS.Checked == true)
                        { Send_buf[1] = 0x81; }

                        Send_buf[2] = byte.Parse(arr.Length.ToString());
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
                        }
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5 + arr.Length - 2], out Send_buf[5 + arr.Length - 1]);
                        try
                        {
                            frmHelp.comPort.Write(Send_buf, 0, 5 + arr.Length);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                    }
                    break;
                case "CH13721_51Reg_W":
                    {
                        Setting.TempRecord.cmdLen = arr.Length + 5;
                        Send_buf = new byte[arr.Length + 5];
                        Send_buf[0] = 0x4A;
                        Send_buf[1] = 0x03;
                        Send_buf[2] = byte.Parse(arr.Length.ToString());
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
                        }
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5 + arr.Length - 2], out Send_buf[5 + arr.Length - 1]);
                        try
                        {
                            frmHelp.comPort.Write(Send_buf, 0, 5 + arr.Length);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                    }
                    break;
                case "CH13721_RegR":
                    {
                        Setting.TempRecord.CH13721CmdType = 0x02;
                        try
                        {
                            DataNum = Convert.ToInt32(CH13721_txtDataNum.Text) + 1;
                            Setting.TempRecord.cmdLen = DataNum + 5;
                            Send_buf = new byte[DataNum + 5];
                            Send_buf[0] = 0x4A; //CH13721
                            Send_buf[1] = 0x02;
                            Send_buf[2] = byte.Parse(Convert.ToString(DataNum));
                            Send_buf[3] = byte.Parse((Convert.ToInt32(arr[0], 16)).ToString());  //cmd
                            CRCData.CalculateCrc16(Send_buf, out Send_buf[5 + DataNum - 2], out Send_buf[5 + DataNum - 1]);
                        }
                        catch
                        {
                            MessageBox.Show("读取长度有误，请检查", "错误");
                            break;
                        }
                        try
                        {
                            frmHelp.comPort.Write(Send_buf, 0, 5 + DataNum);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                    }
                    break;
                default: break;
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        private void CH13721_GAMA_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[22];
            Setting.TempRecord.cmdLen = 22;
            Send_buf[2] = 17;
            Send_buf[3] = 0;
            Send_buf[0] = 0x4A; //CH13721


            switch (btnTmp.Name)
            {
                case "CH13721_GammaR_Red1":
                    {

                        Send_buf[1] = 0x09;                             //读Gamma
                        Send_buf[3] |= 0x00;                            //红色red
                        Send_buf[4] = 0x00;
                        Setting.TempRecord.CH13721CmdType = 0x031;       //      3


                    }
                    break;
                case "CH13721_GammaR_Red2":
                    {

                        Send_buf[1] = 0x09;                             //读Gamma
                        Send_buf[3] |= 0x00;                            //红色red
                        Send_buf[4] = 0x01;
                        Setting.TempRecord.CH13721CmdType = 0x032;       //      3

                    }
                    break;
                case "CH13721_GammaR_Red3":
                    {

                        Send_buf[1] = 0x09;                             //读Gamma
                        Send_buf[3] |= 0x00;                            //红色red
                        Send_buf[4] = 0x02;
                        Setting.TempRecord.CH13721CmdType = 0x033;       //      3

                    }
                    break;
                case "CH13721_GammaR_Green1":
                    {

                        Send_buf[1] = 0x09;                         //  读Gamma
                        Send_buf[3] |= 0x42;                        //绿色 green
                        Send_buf[4] = 0x00;
                        Setting.TempRecord.CH13721CmdType = 0x041;   //      4

                    }
                    break;
                case "CH13721_GammaR_Green2":
                    {

                        Send_buf[1] = 0x09;                         //  读Gamma
                        Send_buf[3] |= 0x42;                        //绿色 green
                        Send_buf[4] = 0x01;
                        Setting.TempRecord.CH13721CmdType = 0x042;   //      4

                    }
                    break;
                case "CH13721_GammaR_Green3":
                    {

                        Send_buf[1] = 0x09;                         //  读Gamma
                        Send_buf[3] |= 0x42;                        //绿色 green
                        Send_buf[4] = 0x02;
                        Setting.TempRecord.CH13721CmdType = 0x043;   //      4

                    }
                    break;
                case "CH13721_GammaR_Blue1": //              9
                    {

                        Send_buf[1] = 0x09;                         //   读Gamma
                        Send_buf[3] |= 0x81;                        //   blue  
                        Send_buf[4] = 0x00;
                        Setting.TempRecord.CH13721CmdType = 0x051;   //      5

                    }
                    break;
                case "CH13721_GammaR_Blue2": //              9
                    {

                        Send_buf[1] = 0x09;                         //   读Gamma
                        Send_buf[3] |= 0x81;                        //   blue  
                        Send_buf[4] = 0x01;
                        Setting.TempRecord.CH13721CmdType = 0x052;   //      5

                    }
                    break;
                case "CH13721_GammaR_Blue3": //              9
                    {

                        Send_buf[1] = 0x09;                         //   读Gamma
                        Send_buf[3] |= 0x81;                        //   blue  
                        Send_buf[4] = 0x02;
                        Setting.TempRecord.CH13721CmdType = 0x053;   //      5

                    }
                    break;
                //--------------------------------------
                case "CH13721_GammaW_Red1":
                    {
                        Send_buf[1] = 0x08;                       //写Gamma
                        Send_buf[3] = 0x00;                       //红色1
                        Send_buf[4] = 0x00;

                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(CH13721_R0.Value.ToString()) >> 8) | ((Convert.ToUInt16(CH13721_R1.Value.ToString()) >> 8) << 2) | ((Convert.ToUInt16(CH13721_R3.Value.ToString()) >> 8) << 4) | ((Convert.ToUInt16(CH13721_R4.Value.ToString()) >> 8) << 6));
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(CH13721_R0.Value.ToString()) & 0xff);
                        Send_buf[7] = Convert.ToByte(Convert.ToUInt16(CH13721_R1.Value.ToString()) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(CH13721_R3.Value.ToString()) & 0xff);
                        Send_buf[9] = Convert.ToByte(Convert.ToUInt16(CH13721_R4.Value.ToString()) & 0xff);

                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(CH13721_R8.Value.ToString()) >> 8) | ((Convert.ToUInt16(CH13721_R12.Value.ToString()) >> 8) << 2) | ((Convert.ToUInt16(CH13721_R16.Value.ToString()) >> 8) << 4) | ((Convert.ToUInt16(CH13721_R20.Value.ToString()) >> 8) << 6));
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(CH13721_R8.Value.ToString()) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(CH13721_R12.Value.ToString()) & 0xff);
                        Send_buf[13] = Convert.ToByte(Convert.ToUInt16(CH13721_R16.Value.ToString()) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(CH13721_R20.Value.ToString()) & 0xff);

                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(CH13721_R28.Value.ToString())) >> 8 | ((Convert.ToUInt16(CH13721_R36.Value.ToString()) >> 8) << 2) | ((Convert.ToUInt16(CH13721_R44.Value.ToString()) >> 8) << 4) | ((Convert.ToUInt16(CH13721_R52.Value.ToString()) >> 8) << 6));
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(CH13721_R28.Value.ToString()) & 0xff);
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(CH13721_R36.Value.ToString()) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(CH13721_R44.Value.ToString()) & 0xff);
                        Send_buf[19] = Convert.ToByte(Convert.ToUInt16(CH13721_R52.Value.ToString()) & 0xff);

                    }
                    break;
                case "CH13721_GammaW_Red2":
                    {
                        Send_buf[1] = 0x08;                       //写Gamma
                        Send_buf[3] = 0x00;                       //红色2
                        Send_buf[4] = 0x01;

                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(CH13721_R68.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_R84.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_R100.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_R116.Value.ToString()) >> 8) << 6);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(CH13721_R68.Value.ToString()) & 0xff);
                        Send_buf[7] = Convert.ToByte(Convert.ToUInt16(CH13721_R84.Value.ToString()) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(CH13721_R100.Value.ToString()) & 0xff);
                        Send_buf[9] = Convert.ToByte(Convert.ToUInt16(CH13721_R116.Value.ToString()) & 0xff);

                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(CH13721_R124.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_R132.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_R148.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_R164.Value.ToString()) >> 8) << 6);
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(CH13721_R124.Value.ToString()) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(CH13721_R132.Value.ToString()) & 0xff);
                        Send_buf[13] = Convert.ToByte(Convert.ToUInt16(CH13721_R148.Value.ToString()) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(CH13721_R164.Value.ToString()) & 0xff);

                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(CH13721_R180.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_R188.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_R196.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_R204.Value.ToString()) >> 8) << 6);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(CH13721_R180.Value.ToString()) & 0xff);
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(CH13721_R188.Value.ToString()) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(CH13721_R196.Value.ToString()) & 0xff);
                        Send_buf[19] = Convert.ToByte(Convert.ToUInt16(CH13721_R204.Value.ToString()) & 0xff);
                    }
                    break;
                case "CH13721_GammaW_Red3":
                    {
                        Send_buf[1] = 0x08;                       //写Gamma
                        Send_buf[3] = 0x00;
                        Send_buf[4] = 0x02;

                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(CH13721_R212.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_R220.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_R228.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_R236.Value.ToString()) >> 8) << 6);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(CH13721_R212.Value.ToString()) & 0xff);
                        Send_buf[7] = Convert.ToByte(Convert.ToUInt16(CH13721_R220.Value.ToString()) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(CH13721_R228.Value.ToString()) & 0xff);
                        Send_buf[9] = Convert.ToByte(Convert.ToUInt16(CH13721_R236.Value.ToString()) & 0xff);

                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(CH13721_R244.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_R248.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_R250.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_R252.Value.ToString()) >> 8) << 6);
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(CH13721_R244.Value.ToString()) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(CH13721_R248.Value.ToString()) & 0xff);
                        Send_buf[13] = Convert.ToByte(Convert.ToUInt16(CH13721_R250.Value.ToString()) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(CH13721_R252.Value.ToString()) & 0xff);

                        Send_buf[15] = Convert.ToByte((((Convert.ToUInt16(CH13721_R254.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_R255.Value.ToString()) >> 8) << 2)) & 0xff);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(CH13721_R254.Value.ToString()) & 0xff);
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(CH13721_R255.Value.ToString()) & 0xff);
                    }
                    break;
                case "CH13721_GammaW_Green1":
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] = 0x42;                       //绿色1
                        Send_buf[4] = 0x00;

                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(CH13721_G0.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_G1.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_G3.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_G4.Value.ToString()) >> 8) << 6);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(CH13721_G0.Value.ToString()) & 0xff);
                        Send_buf[7] = Convert.ToByte(Convert.ToUInt16(CH13721_G1.Value.ToString()) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(CH13721_G3.Value.ToString()) & 0xff);
                        Send_buf[9] = Convert.ToByte(Convert.ToUInt16(CH13721_G4.Value.ToString()) & 0xff);

                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(CH13721_G8.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_G12.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_G16.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_G20.Value.ToString()) >> 8) << 6);
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(CH13721_G8.Value.ToString()) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(CH13721_G12.Value.ToString()) & 0xff);
                        Send_buf[13] = Convert.ToByte(Convert.ToUInt16(CH13721_G16.Value.ToString()) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(CH13721_G20.Value.ToString()) & 0xff);

                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(CH13721_G28.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_G36.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_G44.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_G52.Value.ToString()) >> 8) << 6);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(CH13721_G28.Value.ToString()) & 0xff);
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(CH13721_G36.Value.ToString()) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(CH13721_G44.Value.ToString()) & 0xff);
                        Send_buf[19] = Convert.ToByte(Convert.ToUInt16(CH13721_G52.Value.ToString()) & 0xff);

                    }
                    break;
                case "CH13721_GammaW_Green2":
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] = 0x42;                       //绿色2
                        Send_buf[4] = 0x01;

                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(CH13721_G68.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_G84.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_G100.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_G116.Value.ToString()) >> 8) << 6);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(CH13721_G68.Value.ToString()) & 0xff);
                        Send_buf[7] = Convert.ToByte(Convert.ToUInt16(CH13721_G84.Value.ToString()) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(CH13721_G100.Value.ToString()) & 0xff);
                        Send_buf[9] = Convert.ToByte(Convert.ToUInt16(CH13721_G116.Value.ToString()) & 0xff);

                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(CH13721_G124.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_G132.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_G148.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_G164.Value.ToString()) >> 8) << 6);
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(CH13721_G124.Value.ToString()) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(CH13721_G132.Value.ToString()) & 0xff);
                        Send_buf[13] = Convert.ToByte(Convert.ToUInt16(CH13721_G148.Value.ToString()) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(CH13721_G164.Value.ToString()) & 0xff);

                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(CH13721_G180.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_G188.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_G196.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_G204.Value.ToString()) >> 8) << 6);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(CH13721_G180.Value.ToString()) & 0xff);
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(CH13721_G188.Value.ToString()) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(CH13721_G196.Value.ToString()) & 0xff);
                        Send_buf[19] = Convert.ToByte(Convert.ToUInt16(CH13721_G204.Value.ToString()) & 0xff);
                    }
                    break;
                case "CH13721_GammaW_Green3":
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] = 0x42;                       //绿色3
                        Send_buf[4] = 0x02;

                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(CH13721_G212.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_G220.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_G228.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_G236.Value.ToString()) >> 8) << 6);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(CH13721_G212.Value.ToString()) & 0xff);
                        Send_buf[7] = Convert.ToByte(Convert.ToUInt16(CH13721_G220.Value.ToString()) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(CH13721_G228.Value.ToString()) & 0xff);
                        Send_buf[9] = Convert.ToByte(Convert.ToUInt16(CH13721_G236.Value.ToString()) & 0xff);

                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(CH13721_G244.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_G248.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_G250.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_G252.Value.ToString()) >> 8) << 6);
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(CH13721_G244.Value.ToString()) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(CH13721_G248.Value.ToString()) & 0xff);
                        Send_buf[13] = Convert.ToByte(Convert.ToUInt16(CH13721_G250.Value.ToString()) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(CH13721_G252.Value.ToString()) & 0xff);

                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(CH13721_G254.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_G255.Value.ToString()) >> 8) << 2);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(CH13721_G254.Value.ToString()) & 0xff);
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(CH13721_G255.Value.ToString()) & 0xff);
                    }
                    break;
                case "CH13721_GammaW_Blue1":     //      8
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] = 0x81;
                        Send_buf[4] = 0x00;

                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(CH13721_B0.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_B1.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_B3.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_B4.Value.ToString()) >> 8) << 6);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(CH13721_B0.Value.ToString()) & 0xff);
                        Send_buf[7] = Convert.ToByte(Convert.ToUInt16(CH13721_B1.Value.ToString()) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(CH13721_B3.Value.ToString()) & 0xff);
                        Send_buf[9] = Convert.ToByte(Convert.ToUInt16(CH13721_B4.Value.ToString()) & 0xff);

                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(CH13721_B8.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_B12.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_B16.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_B20.Value.ToString()) >> 8) << 6);
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(CH13721_B8.Value.ToString()) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(CH13721_B12.Value.ToString()) & 0xff);
                        Send_buf[13] = Convert.ToByte(Convert.ToUInt16(CH13721_B16.Value.ToString()) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(CH13721_B20.Value.ToString()) & 0xff);

                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(CH13721_B28.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_B36.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_B44.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_B52.Value.ToString()) >> 8) << 6);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(CH13721_B28.Value.ToString()) & 0xff);
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(CH13721_B36.Value.ToString()) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(CH13721_B44.Value.ToString()) & 0xff);
                        Send_buf[19] = Convert.ToByte(Convert.ToUInt16(CH13721_B52.Value.ToString()) & 0xff);

                    }
                    break;
                case "CH13721_GammaW_Blue2":     //      8
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] = 0x81;
                        Send_buf[4] = 0x01;

                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(CH13721_B68.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_B84.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_B100.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_B116.Value.ToString()) >> 8) << 6);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(CH13721_B68.Value.ToString()) & 0xff);
                        Send_buf[7] = Convert.ToByte(Convert.ToUInt16(CH13721_B84.Value.ToString()) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(CH13721_B100.Value.ToString()) & 0xff);
                        Send_buf[9] = Convert.ToByte(Convert.ToUInt16(CH13721_B116.Value.ToString()) & 0xff);

                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(CH13721_B124.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_B132.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_B148.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_B164.Value.ToString()) >> 8) << 6);
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(CH13721_B124.Value.ToString()) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(CH13721_B132.Value.ToString()) & 0xff);
                        Send_buf[13] = Convert.ToByte(Convert.ToUInt16(CH13721_B148.Value.ToString()) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(CH13721_B164.Value.ToString()) & 0xff);

                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(CH13721_B180.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_B188.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_B196.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_B204.Value.ToString()) >> 8) << 6);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(CH13721_B180.Value.ToString()) & 0xff);
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(CH13721_B188.Value.ToString()) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(CH13721_B196.Value.ToString()) & 0xff);
                        Send_buf[19] = Convert.ToByte(Convert.ToUInt16(CH13721_B204.Value.ToString()) & 0xff);
                    }
                    break;
                case "CH13721_GammaW_Blue3":     //      8
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] = 0x81;
                        Send_buf[4] = 0x02;

                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(CH13721_B212.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_B220.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_B228.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_B236.Value.ToString()) >> 8) << 6);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(CH13721_B212.Value.ToString()) & 0xff);
                        Send_buf[7] = Convert.ToByte(Convert.ToUInt16(CH13721_B220.Value.ToString()) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(CH13721_B228.Value.ToString()) & 0xff);
                        Send_buf[9] = Convert.ToByte(Convert.ToUInt16(CH13721_B236.Value.ToString()) & 0xff);

                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(CH13721_B244.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_B248.Value.ToString()) >> 8) << 2 | (Convert.ToUInt16(CH13721_B250.Value.ToString()) >> 8) << 4 | (Convert.ToUInt16(CH13721_B252.Value.ToString()) >> 8) << 6);
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(CH13721_B244.Value.ToString()) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(CH13721_B248.Value.ToString()) & 0xff);
                        Send_buf[13] = Convert.ToByte(Convert.ToUInt16(CH13721_B250.Value.ToString()) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(CH13721_B252.Value.ToString()) & 0xff);

                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(CH13721_B254.Value.ToString()) >> 8) | (Convert.ToUInt16(CH13721_B255.Value.ToString()) >> 8) << 2);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(CH13721_B254.Value.ToString()) & 0xff);
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(CH13721_B255.Value.ToString()) & 0xff);
                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[20], out Send_buf[21]);
            try
            {
                CH13721_0XFE_XX = 0x00;
                frmHelp.comPort.Write(Send_buf, 0, 22);   //CH13721
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        private void CH13721_trackBar_Scroll(object sender, EventArgs e)
        {
            CH13721_label.Text = CH13721_51Reg_trackBar.Value.ToString() + "阶";
            CH13721_txtRegData.Text = "51," + CH13721_51Reg_trackBar.Value.ToString("x2");
            CH13721_RegWR_Click(CH13721_51Reg_W, e);
        }
        private void CH13721_OTP_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;

            Send_buf[0] = 0x4A;        //CH13721
            Send_buf[2] = 0x02;
            switch (btnTmp.Name)
            {
                case "CH13721_OTP_Gamma": //otp GammaSet      A
                    {
                        CH13721_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0A;
                    }
                    break;
                case "CH13721_OTP_HBM"://otp Gamma HBM       D
                    {
                        CH13721_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0D;
                    }
                    break;
                case "CH13721_OTP_GOA"://otp GOA       E
                    {
                        CH13721_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0E;
                    }
                    break;
                case "CH13721_OTP_ID"://otp ID                   F
                    {
                        CH13721_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0F;
                    }
                    break;
                /*case "CH13721_R_OTPTime":       //          B
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0B;
                        Setting.TempRecord.RM69350CmdType = 0x02;       //      2
                        if (radioButton17.Checked == true)
                        {
                            Send_buf[3] = 0x00;
                        }
                        else if (radioButton16.Checked == true)
                        {
                            Send_buf[3] = 0x01;
                        }
                        else if (radioButton15.Checked == true)
                        {
                            Send_buf[3] = 0x02;
                        }
                        else if (radioButton14.Checked == true)
                        {
                            Send_buf[3] = 0x03;
                        }
                        else if (radioButton13.Checked == true)
                        {
                            Send_buf[3] = 0x04;
                        }
                    }
                    break;
                case "RM69350_W_OTPTime":         //w写寄存器   C
                    {
                        RM350_0XFE_XX = 0x00;
                        if (Convert.ToDecimal(textBox68.Text) > 3)
                        {
                            MessageBox.Show("OTP次数不能大于3次", "警告");
                            return;
                        }
                        else
                        {
                            Send_buf[1] = 0x0C;
                            Send_buf[4] = byte.Parse((Convert.ToInt32(textBox68.Text.Trim(), 16)).ToString());
                        }
                    }
                    break;*/
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 7);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }

        private int[] Parttern_CH13721 = new int[32] { 255, 254, 252, 250, 248, 244, 236, 228, 220, 212, 204, 196, 188, 180, 164, 148, 132, 124, 116, 100, 84, 68, 52, 44, 36, 28, 20, 16, 12, 8, 4, 3 };
        private void btnCalculate_CH13721_Click(object sender, EventArgs e)
        {
            txtCH_255.Text = (Math.Pow((Parttern_CH13721[0] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_254.Text = (Math.Pow((Parttern_CH13721[1] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_252.Text = (Math.Pow((Parttern_CH13721[2] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_250.Text = (Math.Pow((Parttern_CH13721[3] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_248.Text = (Math.Pow((Parttern_CH13721[4] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_244.Text = (Math.Pow((Parttern_CH13721[5] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_236.Text = (Math.Pow((Parttern_CH13721[6] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_228.Text = (Math.Pow((Parttern_CH13721[7] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_220.Text = (Math.Pow((Parttern_CH13721[8] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_212.Text = (Math.Pow((Parttern_CH13721[9] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_204.Text = (Math.Pow((Parttern_CH13721[10] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_196.Text = (Math.Pow((Parttern_CH13721[11] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_188.Text = (Math.Pow((Parttern_CH13721[12] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_180.Text = (Math.Pow((Parttern_CH13721[13] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_164.Text = (Math.Pow((Parttern_CH13721[14] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_148.Text = (Math.Pow((Parttern_CH13721[15] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_132.Text = (Math.Pow((Parttern_CH13721[16] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_124.Text = (Math.Pow((Parttern_CH13721[17] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_116.Text = (Math.Pow((Parttern_CH13721[18] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_100.Text = (Math.Pow((Parttern_CH13721[19] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_84.Text = (Math.Pow((Parttern_CH13721[20] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_68.Text = (Math.Pow((Parttern_CH13721[21] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_52.Text = (Math.Pow((Parttern_CH13721[22] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_44.Text = (Math.Pow((Parttern_CH13721[23] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_36.Text = (Math.Pow((Parttern_CH13721[24] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_28.Text = (Math.Pow((Parttern_CH13721[25] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_20.Text = (Math.Pow((Parttern_CH13721[26] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_16.Text = (Math.Pow((Parttern_CH13721[27] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_12.Text = (Math.Pow((Parttern_CH13721[28] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_8.Text = (Math.Pow((Parttern_CH13721[29] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_4.Text = (Math.Pow((Parttern_CH13721[30] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
            txtCH_3.Text = (Math.Pow((Parttern_CH13721[31] / 255.0), Convert.ToDouble(txtCH_gamma.Text)) * Convert.ToDouble(txtCH_lum.Text)).ToString("F4");
        }

        private void CH13721_Gamma_Load(object sender, EventArgs e)
        {

        }

        private void CH13721_auto_gamma(Object myObject, EventArgs e)
        {
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            frmHelp.Msr_CASDK();

            if ((frmHelp.CASDK_LUM < (D_lum[index] + lum_off)) && (frmHelp.CASDK_LUM > (D_lum[index] - lum_off)))//G
                flag1 = 0;
            if ((frmHelp.CASDK_y < (D_y + y_off)) && (frmHelp.CASDK_y > (D_y - y_off)) && flag1 == 0)//B
                flag2 = 0;
            if ((frmHelp.CASDK_x < (D_x + x_off)) && (frmHelp.CASDK_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//R
                flag3 = 0;

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                frmHelp.T20.Enabled = false;
                index++;
                adjust_Rnum = 0;
                adjust_Gnum = 0;
                adjust_Bnum = 0;
                adjust_l = 0;
                adjust_x = 0;
                adjust_y = 0;
                if (index == 32)
                {
                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "CH13721 gamma调试完毕!\r\n";
                }
                if (index < 32)
                {
                    switch (index)
                    {
                        case 1:
                            lum_off = Convert.ToDouble(textCH_254_off.Text);
                            break;
                        case 2:
                            lum_off = Convert.ToDouble(textCH_252_off.Text);
                            break;
                        case 3:
                            lum_off = Convert.ToDouble(textCH_250_off.Text);
                            break;
                        case 4:
                            lum_off = Convert.ToDouble(textCH_248_off.Text);
                            break;
                        case 5:
                            lum_off = Convert.ToDouble(textCH_244_off.Text);
                            break;
                        case 6:
                            lum_off = Convert.ToDouble(textCH_236_off.Text);
                            break;
                        case 7:
                            lum_off = Convert.ToDouble(textCH_228_off.Text);
                            break;
                        case 8:
                            lum_off = Convert.ToDouble(textCH_220_off.Text);
                            break;
                        case 9:
                            lum_off = Convert.ToDouble(textCH_212_off.Text);
                            break;
                        case 10:
                            lum_off = Convert.ToDouble(textCH_204_off.Text);
                            break;
                        case 11:
                            lum_off = Convert.ToDouble(textCH_196_off.Text);
                            break;
                        case 12:
                            lum_off = Convert.ToDouble(textCH_188_off.Text);
                            break;
                        case 13:
                            lum_off = Convert.ToDouble(textCH_180_off.Text);
                            break;
                        case 14:
                            lum_off = Convert.ToDouble(textCH_164_off.Text);
                            break;
                        case 15:
                            lum_off = Convert.ToDouble(textCH_148_off.Text);
                            break;
                        case 16:
                            lum_off = Convert.ToDouble(textCH_132_off.Text) * Convert.ToDouble(txtCH_132.Text) / 100;
                            break;
                        case 17:
                            lum_off = Convert.ToDouble(textCH_124_off.Text) * Convert.ToDouble(txtCH_124.Text) / 100;
                            break;
                        case 18:
                            lum_off = Convert.ToDouble(textCH_116_off.Text) * Convert.ToDouble(txtCH_116.Text) / 100;
                            break;
                        case 19:
                            lum_off = Convert.ToDouble(textCH_100_off.Text) * Convert.ToDouble(txtCH_100.Text) / 100;
                            break;
                        case 20:
                            lum_off = Convert.ToDouble(textCH_84_off.Text) * Convert.ToDouble(txtCH_84.Text) / 100;
                            break;
                        case 21:
                            lum_off = Convert.ToDouble(textCH_68_off.Text) * Convert.ToDouble(txtCH_68.Text) / 100;
                            break;
                        case 22:
                            lum_off = Convert.ToDouble(textCH_52_off.Text) * Convert.ToDouble(txtCH_52.Text) / 100;
                            break;
                        case 23:
                            lum_off = Convert.ToDouble(textCH_44_off.Text) * Convert.ToDouble(txtCH_44.Text) / 100;
                            break;
                        case 24:
                            lum_off = Convert.ToDouble(textCH_36_off.Text) * Convert.ToDouble(txtCH_36.Text) / 100;
                            break;
                        case 25:
                            lum_off = Convert.ToDouble(textCH_28_off.Text) * Convert.ToDouble(txtCH_28.Text) / 100;
                            break;
                        case 26:
                            lum_off = Convert.ToDouble(textCH_20_off.Text) * Convert.ToDouble(txtCH_20.Text) / 100;
                            break;
                        case 27:
                            {
                                lum_off = Convert.ToDouble(textCH_16_off.Text) * Convert.ToDouble(txtCH_16.Text) / 100;
                                x_off = x_off + 0.001;
                                y_off = y_off + 0.001;
                            }
                            break;
                        case 28:
                            {
                                lum_off = Convert.ToDouble(textCH_12_off.Text) * Convert.ToDouble(txtCH_12.Text) / 100;
                                x_off = x_off + 0.001;
                                y_off = y_off + 0.001;
                            }
                            break;
                        case 29:
                            {
                                lum_off = Convert.ToDouble(textCH_8_off.Text) * Convert.ToDouble(txtCH_8.Text) / 100;
                                x_off = x_off + 0.001;
                                y_off = y_off + 0.001;
                            }
                            break;
                        case 30:
                            {
                                lum_off = Convert.ToDouble(textCH_4_off.Text) * Convert.ToDouble(txtCH_4.Text) / 100;
                                x_off = x_off + 0.001;
                                y_off = y_off + 0.001;
                            }
                            break;
                        case 31:
                            {
                                lum_off = Convert.ToDouble(textCH_3_off.Text) * Convert.ToDouble(txtCH_3.Text) / 100;
                                x_off = x_off + 0.001;
                                y_off = y_off + 0.001;
                            }
                            break;
                    }
                    frmHelp.numPatternR.Value = Parttern_CH13721[index];
                    frmHelp.numPatternG.Value = Parttern_CH13721[index];
                    frmHelp.numPatternB.Value = Parttern_CH13721[index];
                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, e);//写入Pattern参数
                    Thread.Sleep(10); //1秒=1000毫秒，
                    frmHelp.T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else
            {
                Lr_t = CaSDKObj.R_y * ((frmHelp.CASDK_y - CaSDKObj.G_y) * (CaSDKObj.B_x - frmHelp.CASDK_x) + (frmHelp.CASDK_x - CaSDKObj.G_x) * (frmHelp.CASDK_y - CaSDKObj.B_y)) / CaSDKObj.B_y / ((frmHelp.CASDK_y - CaSDKObj.G_y) * (frmHelp.CASDK_x - CaSDKObj.R_x) + (frmHelp.CASDK_x - CaSDKObj.G_x) * (CaSDKObj.R_y - frmHelp.CASDK_y));
                Lb_t = 1.0;
                Lg_t = CaSDKObj.G_y * ((frmHelp.CASDK_y - CaSDKObj.R_y) * (CaSDKObj.B_x - frmHelp.CASDK_x) + (frmHelp.CASDK_x - CaSDKObj.R_x) * (frmHelp.CASDK_y - CaSDKObj.B_y)) / CaSDKObj.B_y / ((frmHelp.CASDK_y - CaSDKObj.R_y) * (frmHelp.CASDK_x - CaSDKObj.G_x) + (frmHelp.CASDK_x - CaSDKObj.R_x) * (CaSDKObj.G_y - frmHelp.CASDK_y));
                lumr = frmHelp.CASDK_LUM * Lr_t / (Lr_t + Lg_t + Lb_t);
                lumg = frmHelp.CASDK_LUM * Lg_t / (Lr_t + Lg_t + Lb_t);
                lumb = frmHelp.CASDK_LUM * Lb_t / (Lr_t + Lg_t + Lb_t);

                switch (index)
                {
                    case 0:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R255.Value = CH13721_R255.Value - step_h;
                                    else CH13721_R255.Value = CH13721_R255.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R255.Value = CH13721_R255.Value - step_l;
                                    else CH13721_R255.Value = CH13721_R255.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G255.Value = CH13721_G255.Value - step_h;
                                    else CH13721_G255.Value = CH13721_G255.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G255.Value = CH13721_G255.Value - step_l;
                                    else CH13721_G255.Value = CH13721_G255.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B255.Value = CH13721_B255.Value - step_h;
                                    else CH13721_B255.Value = CH13721_B255.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B255.Value = CH13721_B255.Value - step_l;
                                    else CH13721_B255.Value = CH13721_B255.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue3, e);
                        }
                        break;
                    case 1:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R254.Value = CH13721_R254.Value - step_h;
                                    else CH13721_R254.Value = CH13721_R254.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R254.Value = CH13721_R254.Value - step_l;
                                    else CH13721_R254.Value = CH13721_R254.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G254.Value = CH13721_G254.Value - step_h;
                                    else CH13721_G254.Value = CH13721_G254.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G254.Value = CH13721_G254.Value - step_l;
                                    else CH13721_G254.Value = CH13721_G254.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B254.Value = CH13721_B254.Value - step_h;
                                    else CH13721_B254.Value = CH13721_B254.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B254.Value = CH13721_B254.Value - step_l;
                                    else CH13721_B254.Value = CH13721_B254.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue3, e);
                        }
                        break;
                    case 2:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R252.Value = CH13721_R252.Value - step_h;
                                    else CH13721_R252.Value = CH13721_R252.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R252.Value = CH13721_R252.Value - step_l;
                                    else CH13721_R252.Value = CH13721_R252.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G252.Value = CH13721_G252.Value - step_h;
                                    else CH13721_G252.Value = CH13721_G252.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G252.Value = CH13721_G252.Value - step_l;
                                    else CH13721_G252.Value = CH13721_G252.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B252.Value = CH13721_B252.Value - step_h;
                                    else CH13721_B252.Value = CH13721_B252.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B252.Value = CH13721_B252.Value - step_l;
                                    else CH13721_B252.Value = CH13721_B252.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue3, e);
                        }
                        break;
                    case 3:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R250.Value = CH13721_R250.Value - step_h;
                                    else CH13721_R250.Value = CH13721_R250.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R250.Value = CH13721_R250.Value - step_l;
                                    else CH13721_R250.Value = CH13721_R250.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G250.Value = CH13721_G250.Value - step_h;
                                    else CH13721_G250.Value = CH13721_G250.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G250.Value = CH13721_G250.Value - step_l;
                                    else CH13721_G250.Value = CH13721_G250.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B250.Value = CH13721_B250.Value - step_h;
                                    else CH13721_B250.Value = CH13721_B250.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B250.Value = CH13721_B250.Value - step_l;
                                    else CH13721_B250.Value = CH13721_B250.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue3, e);
                        }
                        break;
                    case 4:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R248.Value = CH13721_R248.Value - step_h;
                                    else CH13721_R248.Value = CH13721_R248.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R248.Value = CH13721_R248.Value - step_l;
                                    else CH13721_R248.Value = CH13721_R248.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G248.Value = CH13721_G248.Value - step_h;
                                    else CH13721_G248.Value = CH13721_G248.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G248.Value = CH13721_G248.Value - step_l;
                                    else CH13721_G248.Value = CH13721_G248.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B248.Value = CH13721_B248.Value - step_h;
                                    else CH13721_B248.Value = CH13721_B248.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B248.Value = CH13721_B248.Value - step_l;
                                    else CH13721_B248.Value = CH13721_B248.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue3, e);
                        }
                        break;
                    case 5:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R244.Value = CH13721_R244.Value - step_h;
                                    else CH13721_R244.Value = CH13721_R244.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R244.Value = CH13721_R244.Value - step_l;
                                    else CH13721_R244.Value = CH13721_R244.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G244.Value = CH13721_G244.Value - step_h;
                                    else CH13721_G244.Value = CH13721_G244.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G244.Value = CH13721_G244.Value - step_l;
                                    else CH13721_G244.Value = CH13721_G244.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B244.Value = CH13721_B244.Value - step_h;
                                    else CH13721_B244.Value = CH13721_B244.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B244.Value = CH13721_B244.Value - step_l;
                                    else CH13721_B244.Value = CH13721_B244.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue3, e);
                        }
                        break;
                    case 6:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R236.Value = CH13721_R236.Value - step_h;
                                    else CH13721_R236.Value = CH13721_R236.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R236.Value = CH13721_R236.Value - step_l;
                                    else CH13721_R236.Value = CH13721_R236.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G236.Value = CH13721_G236.Value - step_h;
                                    else CH13721_G236.Value = CH13721_G236.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G236.Value = CH13721_G236.Value - step_l;
                                    else CH13721_G236.Value = CH13721_G236.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B236.Value = CH13721_B236.Value - step_h;
                                    else CH13721_B236.Value = CH13721_B236.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B236.Value = CH13721_B236.Value - step_l;
                                    else CH13721_B236.Value = CH13721_B236.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue3, e);
                        }
                        break;
                    case 7:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R228.Value = CH13721_R228.Value - step_h;
                                    else CH13721_R228.Value = CH13721_R228.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R228.Value = CH13721_R228.Value - step_l;
                                    else CH13721_R228.Value = CH13721_R228.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G228.Value = CH13721_G228.Value - step_h;
                                    else CH13721_G228.Value = CH13721_G228.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G228.Value = CH13721_G228.Value - step_l;
                                    else CH13721_G228.Value = CH13721_G228.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B228.Value = CH13721_B228.Value - step_h;
                                    else CH13721_B228.Value = CH13721_B228.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B228.Value = CH13721_B228.Value - step_l;
                                    else CH13721_B228.Value = CH13721_B228.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue3, e);
                        }
                        break;
                    case 8:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R220.Value = CH13721_R220.Value - step_h;
                                    else CH13721_R220.Value = CH13721_R220.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R220.Value = CH13721_R220.Value - step_l;
                                    else CH13721_R220.Value = CH13721_R220.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G220.Value = CH13721_G220.Value - step_h;
                                    else CH13721_G220.Value = CH13721_G220.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G220.Value = CH13721_G220.Value - step_l;
                                    else CH13721_G220.Value = CH13721_G220.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B220.Value = CH13721_B220.Value - step_h;
                                    else CH13721_B220.Value = CH13721_B220.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B220.Value = CH13721_B220.Value - step_l;
                                    else CH13721_B220.Value = CH13721_B220.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue3, e);
                        }
                        break;
                    case 9:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R212.Value = CH13721_R212.Value - step_h;
                                    else CH13721_R212.Value = CH13721_R212.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R212.Value = CH13721_R212.Value - step_l;
                                    else CH13721_R212.Value = CH13721_R212.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G212.Value = CH13721_G212.Value - step_h;
                                    else CH13721_G212.Value = CH13721_G212.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G212.Value = CH13721_G212.Value - step_l;
                                    else CH13721_G212.Value = CH13721_G212.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B212.Value = CH13721_B212.Value - step_h;
                                    else CH13721_B212.Value = CH13721_B212.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B212.Value = CH13721_B212.Value - step_l;
                                    else CH13721_B212.Value = CH13721_B212.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green3, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue3, e);
                        }
                        break;
                    case 10:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R204.Value = CH13721_R204.Value - step_h;
                                    else CH13721_R204.Value = CH13721_R204.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R204.Value = CH13721_R204.Value - step_l;
                                    else CH13721_R204.Value = CH13721_R204.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G204.Value = CH13721_G204.Value - step_h;
                                    else CH13721_G204.Value = CH13721_G204.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G204.Value = CH13721_G204.Value - step_l;
                                    else CH13721_G204.Value = CH13721_G204.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B204.Value = CH13721_B204.Value - step_h;
                                    else CH13721_B204.Value = CH13721_B204.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B204.Value = CH13721_B204.Value - step_l;
                                    else CH13721_B204.Value = CH13721_B204.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue2, e);
                        }
                        break;
                    case 11:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R196.Value = CH13721_R196.Value - step_h;
                                    else CH13721_R196.Value = CH13721_R196.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R196.Value = CH13721_R196.Value - step_l;
                                    else CH13721_R196.Value = CH13721_R196.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G196.Value = CH13721_G196.Value - step_h;
                                    else CH13721_G196.Value = CH13721_G196.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G196.Value = CH13721_G196.Value - step_l;
                                    else CH13721_G196.Value = CH13721_G196.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B196.Value = CH13721_B196.Value - step_h;
                                    else CH13721_B196.Value = CH13721_B196.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B196.Value = CH13721_B196.Value - step_l;
                                    else CH13721_B196.Value = CH13721_B196.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue2, e);
                        }
                        break;
                    case 12:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R188.Value = CH13721_R188.Value - step_h;
                                    else CH13721_R188.Value = CH13721_R188.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R188.Value = CH13721_R188.Value - step_l;
                                    else CH13721_R188.Value = CH13721_R188.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G188.Value = CH13721_G188.Value - step_h;
                                    else CH13721_G188.Value = CH13721_G188.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G188.Value = CH13721_G188.Value - step_l;
                                    else CH13721_G188.Value = CH13721_G188.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B188.Value = CH13721_B188.Value - step_h;
                                    else CH13721_B188.Value = CH13721_B188.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B188.Value = CH13721_B188.Value - step_l;
                                    else CH13721_B188.Value = CH13721_B188.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue2, e);
                        }
                        break;
                    case 13:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R180.Value = CH13721_R180.Value - step_h;
                                    else CH13721_R180.Value = CH13721_R180.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R180.Value = CH13721_R180.Value - step_l;
                                    else CH13721_R180.Value = CH13721_R180.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G180.Value = CH13721_G180.Value - step_h;
                                    else CH13721_G180.Value = CH13721_G180.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G180.Value = CH13721_G180.Value - step_l;
                                    else CH13721_G180.Value = CH13721_G180.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B180.Value = CH13721_B180.Value - step_h;
                                    else CH13721_B180.Value = CH13721_B180.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B180.Value = CH13721_B180.Value - step_l;
                                    else CH13721_B180.Value = CH13721_B180.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue2, e);
                        }
                        break;
                    case 14:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R164.Value = CH13721_R164.Value - step_h;
                                    else CH13721_R164.Value = CH13721_R164.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R164.Value = CH13721_R164.Value - step_l;
                                    else CH13721_R164.Value = CH13721_R164.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G164.Value = CH13721_G164.Value - step_h;
                                    else CH13721_G164.Value = CH13721_G164.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G164.Value = CH13721_G164.Value - step_l;
                                    else CH13721_G164.Value = CH13721_G164.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B164.Value = CH13721_B164.Value - step_h;
                                    else CH13721_B164.Value = CH13721_B164.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B164.Value = CH13721_B164.Value - step_l;
                                    else CH13721_B164.Value = CH13721_B164.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue2, e);
                        }
                        break;
                    case 15:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R148.Value = CH13721_R148.Value - step_h;
                                    else CH13721_R148.Value = CH13721_R148.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R148.Value = CH13721_R148.Value - step_l;
                                    else CH13721_R148.Value = CH13721_R148.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G148.Value = CH13721_G148.Value - step_h;
                                    else CH13721_G148.Value = CH13721_G148.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G148.Value = CH13721_G148.Value - step_l;
                                    else CH13721_G148.Value = CH13721_G148.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B148.Value = CH13721_B148.Value - step_h;
                                    else CH13721_B148.Value = CH13721_B148.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B148.Value = CH13721_B148.Value - step_l;
                                    else CH13721_B148.Value = CH13721_B148.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue2, e);
                        }
                        break;
                    case 16:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R132.Value = CH13721_R132.Value - step_h;
                                    else CH13721_R132.Value = CH13721_R132.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R132.Value = CH13721_R132.Value - step_l;
                                    else CH13721_R132.Value = CH13721_R132.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G132.Value = CH13721_G132.Value - step_h;
                                    else CH13721_G132.Value = CH13721_G132.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G132.Value = CH13721_G132.Value - step_l;
                                    else CH13721_G132.Value = CH13721_G132.Value + step_l;
                                }
                                adjust_Gnum++;
                            }

                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B132.Value = CH13721_B132.Value - step_h;
                                    else CH13721_B132.Value = CH13721_B132.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B132.Value = CH13721_B132.Value - step_l;
                                    else CH13721_B132.Value = CH13721_B132.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue2, e);
                        }
                        break;
                    case 17:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R124.Value = CH13721_R124.Value - step_h;
                                    else CH13721_R124.Value = CH13721_R124.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R124.Value = CH13721_R124.Value - step_l;
                                    else CH13721_R124.Value = CH13721_R124.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G124.Value = CH13721_G124.Value - step_h;
                                    else CH13721_G124.Value = CH13721_G124.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G124.Value = CH13721_G124.Value - step_l;
                                    else CH13721_G124.Value = CH13721_G124.Value + step_l;
                                }
                                adjust_Gnum++;
                            }

                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B124.Value = CH13721_B124.Value - step_h;
                                    else CH13721_B124.Value = CH13721_B124.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B124.Value = CH13721_B124.Value - step_l;
                                    else CH13721_B124.Value = CH13721_B124.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue2, e);
                        }
                        break;
                    case 18:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R116.Value = CH13721_R116.Value - step_h;
                                    else CH13721_R116.Value = CH13721_R116.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R116.Value = CH13721_R116.Value - step_l;
                                    else CH13721_R116.Value = CH13721_R116.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G116.Value = CH13721_G116.Value - step_h;
                                    else CH13721_G116.Value = CH13721_G116.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G116.Value = CH13721_G116.Value - step_l;
                                    else CH13721_G116.Value = CH13721_G116.Value + step_l;
                                }
                                adjust_Gnum++;
                            }

                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B116.Value = CH13721_B116.Value - step_h;
                                    else CH13721_B116.Value = CH13721_B116.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B116.Value = CH13721_B116.Value - step_l;
                                    else CH13721_B116.Value = CH13721_B116.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue2, e);
                        }
                        break;
                    case 19:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R100.Value = CH13721_R100.Value - step_h;
                                    else CH13721_R100.Value = CH13721_R100.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R100.Value = CH13721_R100.Value - step_l;
                                    else CH13721_R100.Value = CH13721_R100.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G100.Value = CH13721_G100.Value - step_h;
                                    else CH13721_G100.Value = CH13721_G100.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G100.Value = CH13721_G100.Value - step_l;
                                    else CH13721_G100.Value = CH13721_G100.Value + step_l;
                                }
                                adjust_Gnum++;
                            }

                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B100.Value = CH13721_B100.Value - step_h;
                                    else CH13721_B100.Value = CH13721_B100.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B100.Value = CH13721_B100.Value - step_l;
                                    else CH13721_B100.Value = CH13721_B100.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue2, e);
                        }
                        break;
                    case 20:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R84.Value = CH13721_R84.Value - step_h;
                                    else CH13721_R84.Value = CH13721_R84.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R84.Value = CH13721_R84.Value - step_l;
                                    else CH13721_R84.Value = CH13721_R84.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G84.Value = CH13721_G84.Value - step_h;
                                    else CH13721_G84.Value = CH13721_G84.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G84.Value = CH13721_G84.Value - step_l;
                                    else CH13721_G84.Value = CH13721_G84.Value + step_l;
                                }
                                adjust_Gnum++;
                            }

                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B84.Value = CH13721_B84.Value - step_h;
                                    else CH13721_B84.Value = CH13721_B84.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B84.Value = CH13721_B84.Value - step_l;
                                    else CH13721_B84.Value = CH13721_B84.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue2, e);
                        }
                        break;
                    case 21:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R68.Value = CH13721_R68.Value - step_h;
                                    else CH13721_R68.Value = CH13721_R68.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R68.Value = CH13721_R68.Value - step_l;
                                    else CH13721_R68.Value = CH13721_R68.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G68.Value = CH13721_G68.Value - step_h;
                                    else CH13721_G68.Value = CH13721_G68.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G68.Value = CH13721_G68.Value - step_l;
                                    else CH13721_G68.Value = CH13721_G68.Value + step_l;
                                }
                                adjust_Gnum++;
                            }

                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B68.Value = CH13721_B68.Value - step_h;
                                    else CH13721_B68.Value = CH13721_B68.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B68.Value = CH13721_B68.Value - step_l;
                                    else CH13721_B68.Value = CH13721_B68.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green2, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue2, e);
                        }
                        break;
                    case 22:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R52.Value = CH13721_R52.Value - step_h;
                                    else CH13721_R52.Value = CH13721_R52.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R52.Value = CH13721_R52.Value - step_l;
                                    else CH13721_R52.Value = CH13721_R52.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G52.Value = CH13721_G52.Value - step_h;
                                    else CH13721_G52.Value = CH13721_G52.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G52.Value = CH13721_G52.Value - step_l;
                                    else CH13721_G52.Value = CH13721_G52.Value + step_l;
                                }
                                adjust_Gnum++;
                            }

                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B52.Value = CH13721_B52.Value - step_h;
                                    else CH13721_B52.Value = CH13721_B52.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B52.Value = CH13721_B52.Value - step_l;
                                    else CH13721_B52.Value = CH13721_B52.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue1, e);
                        }
                        break;
                    case 23:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R44.Value = CH13721_R44.Value - step_h;
                                    else CH13721_R44.Value = CH13721_R44.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R44.Value = CH13721_R44.Value - step_l;
                                    else CH13721_R44.Value = CH13721_R44.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G44.Value = CH13721_G44.Value - step_h;
                                    else CH13721_G44.Value = CH13721_G44.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G44.Value = CH13721_G44.Value - step_l;
                                    else CH13721_G44.Value = CH13721_G44.Value + step_l;
                                }
                                adjust_Gnum++;
                            }

                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B44.Value = CH13721_B44.Value - step_h;
                                    else CH13721_B44.Value = CH13721_B44.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B44.Value = CH13721_B44.Value - step_l;
                                    else CH13721_B44.Value = CH13721_B44.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue1, e);
                        }
                        break;
                    case 24:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R36.Value = CH13721_R36.Value - step_h;
                                    else CH13721_R36.Value = CH13721_R36.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R36.Value = CH13721_R36.Value - step_l;
                                    else CH13721_R36.Value = CH13721_R36.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G36.Value = CH13721_G36.Value - step_h;
                                    else CH13721_G36.Value = CH13721_G36.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G36.Value = CH13721_G36.Value - step_l;
                                    else CH13721_G36.Value = CH13721_G36.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B36.Value = CH13721_B36.Value - step_h;
                                    else CH13721_B36.Value = CH13721_B36.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B36.Value = CH13721_B36.Value - step_l;
                                    else CH13721_B36.Value = CH13721_B36.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue1, e);
                        }
                        break;
                    case 25:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R28.Value = CH13721_R28.Value - step_h;
                                    else CH13721_R28.Value = CH13721_R28.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R28.Value = CH13721_R28.Value - step_l;
                                    else CH13721_R28.Value = CH13721_R28.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G28.Value = CH13721_G28.Value - step_h;
                                    else CH13721_G28.Value = CH13721_G28.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G28.Value = CH13721_G28.Value - step_l;
                                    else CH13721_G28.Value = CH13721_G28.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B28.Value = CH13721_B28.Value - step_h;
                                    else CH13721_B28.Value = CH13721_B28.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B28.Value = CH13721_B28.Value - step_l;
                                    else CH13721_B28.Value = CH13721_B28.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue1, e);
                        }
                        break;
                    case 26://20阶以下
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R20.Value = CH13721_R20.Value - step_h;
                                    else CH13721_R20.Value = CH13721_R20.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R20.Value = CH13721_R20.Value - step_l;
                                    else CH13721_R20.Value = CH13721_R20.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G20.Value = CH13721_G20.Value - step_h;
                                    else CH13721_G20.Value = CH13721_G20.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G20.Value = CH13721_G20.Value - step_l;
                                    else CH13721_G20.Value = CH13721_G20.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B20.Value = CH13721_B20.Value - step_h;
                                    else CH13721_B20.Value = CH13721_B20.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B20.Value = CH13721_B20.Value - step_l;
                                    else CH13721_B20.Value = CH13721_B20.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue1, e);
                        }
                        break;
                    case 27:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R16.Value = CH13721_R16.Value - step_h;
                                    else CH13721_R16.Value = CH13721_R16.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R16.Value = CH13721_R16.Value - step_l;
                                    else CH13721_R16.Value = CH13721_R16.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G16.Value = CH13721_G16.Value - step_h;
                                    else CH13721_G16.Value = CH13721_G16.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G16.Value = CH13721_G16.Value - step_l;
                                    else CH13721_G16.Value = CH13721_G16.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B16.Value = CH13721_B16.Value - step_h;
                                    else CH13721_B16.Value = CH13721_B16.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B16.Value = CH13721_B16.Value - step_l;
                                    else CH13721_B16.Value = CH13721_B16.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue1, e);
                        }
                        break;
                    case 28:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R12.Value = CH13721_R12.Value - step_h;
                                    else CH13721_R12.Value = CH13721_R12.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R12.Value = CH13721_R12.Value - step_l;
                                    else CH13721_R12.Value = CH13721_R12.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G12.Value = CH13721_G12.Value - step_h;
                                    else CH13721_G12.Value = CH13721_G12.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G12.Value = CH13721_G12.Value - step_l;
                                    else CH13721_G12.Value = CH13721_G12.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B12.Value = CH13721_B12.Value - step_h;
                                    else CH13721_B12.Value = CH13721_B12.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B12.Value = CH13721_B12.Value - step_l;
                                    else CH13721_B12.Value = CH13721_B12.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue1, e);
                        }
                        break;
                    case 29:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R8.Value = CH13721_R8.Value - step_h;
                                    else CH13721_R8.Value = CH13721_R8.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R8.Value = CH13721_R8.Value - step_l;
                                    else CH13721_R8.Value = CH13721_R8.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G8.Value = CH13721_G8.Value - step_h;
                                    else CH13721_G8.Value = CH13721_G8.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G8.Value = CH13721_G8.Value - step_l;
                                    else CH13721_G8.Value = CH13721_G8.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B8.Value = CH13721_B8.Value - step_h;
                                    else CH13721_B8.Value = CH13721_B8.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B8.Value = CH13721_B8.Value - step_l;
                                    else CH13721_B8.Value = CH13721_B8.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue1, e);
                        }
                        break;
                    case 30:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R4.Value = CH13721_R4.Value - step_h;
                                    else CH13721_R4.Value = CH13721_R4.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R4.Value = CH13721_R4.Value - step_l;
                                    else CH13721_R4.Value = CH13721_R4.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G4.Value = CH13721_G4.Value - step_h;
                                    else CH13721_G4.Value = CH13721_G4.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G4.Value = CH13721_G4.Value - step_l;
                                    else CH13721_G4.Value = CH13721_G4.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B4.Value = CH13721_B4.Value - step_h;
                                    else CH13721_B4.Value = CH13721_B4.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B4.Value = CH13721_B4.Value - step_l;
                                    else CH13721_B4.Value = CH13721_B4.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue1, e);
                        }
                        break;
                    case 31:
                        {
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R3.Value = CH13721_R3.Value - step_h;
                                    else CH13721_R3.Value = CH13721_R3.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }
                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        CH13721_R3.Value = CH13721_R3.Value - step_l;
                                    else CH13721_R3.Value = CH13721_R3.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G3.Value = CH13721_G3.Value - step_h;
                                    else CH13721_G3.Value = CH13721_G3.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        CH13721_G3.Value = CH13721_G3.Value - step_l;
                                    else CH13721_G3.Value = CH13721_G3.Value + step_l;
                                }
                                adjust_Gnum++;
                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B3.Value = CH13721_B3.Value - step_h;
                                    else CH13721_B3.Value = CH13721_B3.Value + step_h;
                                    adjust_Bnum = 0;
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }
                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        CH13721_B3.Value = CH13721_B3.Value - step_l;
                                    else CH13721_B3.Value = CH13721_B3.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            CH13721_GAMA_Click(CH13721_GammaW_Red1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Green1, e);
                            CH13721_GAMA_Click(CH13721_GammaW_Blue1, e);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        private void btnAutoGammas_CH13721_Click(object sender, EventArgs e)
        {
            int flag = 1;
            double Lr_d, Lg_d, Lb_d;
            D_lums = new double[32, 3];
            D_lum = new double[32];
            index = 0;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;

            D_lum[0] = Convert.ToDouble(txtCH_255.Text);
            D_lum[1] = Convert.ToDouble(txtCH_254.Text);
            D_lum[2] = Convert.ToDouble(txtCH_252.Text);
            D_lum[3] = Convert.ToDouble(txtCH_250.Text);
            D_lum[4] = Convert.ToDouble(txtCH_248.Text);
            D_lum[5] = Convert.ToDouble(txtCH_244.Text);
            D_lum[6] = Convert.ToDouble(txtCH_236.Text);
            D_lum[7] = Convert.ToDouble(txtCH_228.Text);
            D_lum[8] = Convert.ToDouble(txtCH_220.Text);
            D_lum[9] = Convert.ToDouble(txtCH_212.Text);
            D_lum[10] = Convert.ToDouble(txtCH_204.Text);
            D_lum[11] = Convert.ToDouble(txtCH_196.Text);
            D_lum[12] = Convert.ToDouble(txtCH_188.Text);
            D_lum[13] = Convert.ToDouble(txtCH_180.Text);
            D_lum[14] = Convert.ToDouble(txtCH_164.Text);
            D_lum[15] = Convert.ToDouble(txtCH_148.Text);
            D_lum[16] = Convert.ToDouble(txtCH_132.Text);
            D_lum[17] = Convert.ToDouble(txtCH_124.Text);
            D_lum[18] = Convert.ToDouble(txtCH_116.Text);
            D_lum[19] = Convert.ToDouble(txtCH_100.Text);
            D_lum[20] = Convert.ToDouble(txtCH_84.Text);
            D_lum[21] = Convert.ToDouble(txtCH_68.Text);
            D_lum[22] = Convert.ToDouble(txtCH_52.Text);
            D_lum[23] = Convert.ToDouble(txtCH_44.Text);
            D_lum[24] = Convert.ToDouble(txtCH_36.Text);
            D_lum[25] = Convert.ToDouble(txtCH_28.Text);
            D_lum[26] = Convert.ToDouble(txtCH_20.Text);
            D_lum[27] = Convert.ToDouble(txtCH_16.Text);
            D_lum[28] = Convert.ToDouble(txtCH_12.Text);
            D_lum[29] = Convert.ToDouble(txtCH_8.Text);
            D_lum[30] = Convert.ToDouble(txtCH_4.Text);
            D_lum[31] = Convert.ToDouble(txtCH_3.Text);
            D_x = Convert.ToDouble(txtCH_x.Text);
            D_y = Convert.ToDouble(txtCH_y.Text);
            Lr_d = CaSDKObj.R_y * ((D_y - CaSDKObj.G_y) * (CaSDKObj.B_x - D_x) + (D_x - CaSDKObj.G_x) * (D_y - CaSDKObj.B_y)) / CaSDKObj.B_y / ((D_y - CaSDKObj.G_y) * (D_x - CaSDKObj.R_x) + (D_x - CaSDKObj.G_x) * (CaSDKObj.R_y - D_y));
            Lb_d = 1.0;
            Lg_d = CaSDKObj.G_y * ((D_y - CaSDKObj.R_y) * (CaSDKObj.B_x - D_x) + (D_x - CaSDKObj.R_x) * (D_y - CaSDKObj.B_y)) / CaSDKObj.B_y / ((D_y - CaSDKObj.R_y) * (D_x - CaSDKObj.G_x) + (D_x - CaSDKObj.R_x) * (CaSDKObj.G_y - D_y));

            for (int i = 0; i < 32; i++)
            {
                D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
            }

            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        lum_off = Convert.ToDouble(txtCH_loff.Text);
                        x_off = Convert.ToDouble(txtCH_xoff.Text);
                        y_off = Convert.ToDouble(txtCH_yoff.Text);
                        CH13721_GAMA_Click(CH13721_GammaW_Red1, e);
                        CH13721_GAMA_Click(CH13721_GammaW_Red2, e);
                        CH13721_GAMA_Click(CH13721_GammaW_Red3, e);

                        CH13721_GAMA_Click(CH13721_GammaW_Green1, e);
                        CH13721_GAMA_Click(CH13721_GammaW_Green2, e);
                        CH13721_GAMA_Click(CH13721_GammaW_Green3, e);

                        CH13721_GAMA_Click(CH13721_GammaW_Blue1, e);
                        CH13721_GAMA_Click(CH13721_GammaW_Blue2, e);
                        CH13721_GAMA_Click(CH13721_GammaW_Blue3, e);
                        Application.DoEvents();
                        frmHelp.OLED_Show_Click(frmHelp.btnwhite, e);
                        frmHelp.T20.Enabled = true;
                    }
                    break;
                case 2://寄存器值与亮度反比
                    break;
                default:
                    break;
            }
        }

        private void btnCHStop_Click(object sender, EventArgs e)
        {
            frmHelp.T20.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;
        }

    }
}
